package com.sundi.ecmarket.web;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sundi.ecmarket.dto.BrandDto;
import com.sundi.ecmarket.dto.IssueDto;
import com.sundi.ecmarket.dto.KeywordDto;
import com.sundi.ecmarket.dto.ResultEntity;
import com.sundi.ecmarket.entity.EcIssue;
import com.sundi.ecmarket.entity.EcKeyword;
import com.sundi.ecmarket.service.IEcKeywordService;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.xml.transform.Result;
import java.io.ByteArrayOutputStream;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 关键字表 前端控制器
 * </p>
 *
 * @author Mht
 * @since 2020-02-23
 */
@RestController
@Slf4j
@RequestMapping("/ecKeyword")
public class EcKeywordController {


    @Autowired
    IEcKeywordService iEcKeywordService;

    /**
     * @deprecated ec商城中商场管理里的关键字问题修改列表查询模块
     * @author 许家宝
     * @param keywordDto
     * @return
     */
    @RequestMapping("/xujiabao/keyList")
    public ResultEntity list(KeywordDto keywordDto){

        PageHelper.startPage(keywordDto.getPageNo(),keywordDto.getPageSize());
        List<EcKeyword> ecKeywords = iEcKeywordService.selectKeywordList(keywordDto);
        PageInfo info = new PageInfo(ecKeywords);
        return ResultEntity.ok(info);
    }


    /**
     * @deprecated ec商城中商场管理里的关键字问题逻辑删除模块
     * @author 许家宝
     * @param id
     * @return
     */
    @RequestMapping("/xujiabao/logicdel")
    public ResultEntity del(Integer id){

        boolean b = iEcKeywordService.deleteByKey(id);
        return ResultEntity.ok(b);
    }

    /**
     * @deprecated ec商城中商场管理里的关键字问题添加模块
     * @author  许家宝
     * @param keywordDto
     * @return
     */
    @RequestMapping("/xujiabao/keyadd")
    public ResultEntity add(@RequestBody KeywordDto keywordDto){
        System.err.println(keywordDto);
        EcKeyword ecKeyword = new EcKeyword();
        BeanUtils.copyProperties(keywordDto,ecKeyword);
        if(ecKeyword.getId()==null){
            ecKeyword.setAddTime(new Date());
        }
        boolean insert = iEcKeywordService.insert(ecKeyword);
        return ResultEntity.ok(insert);
    }


    /**
     * @deprecated ec商城中商场管理里的关键字问题修改模块
     * @author  许家宝
     * @param keywordDto
     * @return
     */
    @RequestMapping("/xujiabao/keyUpd")
    public ResultEntity upd(@RequestBody KeywordDto keywordDto){
        System.err.println(keywordDto);
        EcKeyword ecKeyword = new EcKeyword();
        BeanUtils.copyProperties(keywordDto,ecKeyword);
        if(ecKeyword.getId()!=null){
            ecKeyword.setUpdateTime(new Date());
        }
        boolean b = iEcKeywordService.updateById(ecKeyword);
        return ResultEntity.ok(b);
    }

    @RequestMapping("/downLoadGoodsExcel")
    public ResponseEntity downLoadGoodsExcel(KeywordDto keywordDto) throws Exception {

        XSSFWorkbook xSSFWorkbooks = iEcKeywordService.downLoadGoodsExcel(keywordDto);
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        xSSFWorkbooks.write(byteArrayOutputStream);

        HttpHeaders httpHeaders =  new HttpHeaders();
        httpHeaders.setContentDispositionFormData("attachment", new String("关键字表.xlsx".getBytes("UTF-8"), "ISO-8859-1"));
        httpHeaders.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        return new ResponseEntity(byteArrayOutputStream.toByteArray(),httpHeaders, HttpStatus.CREATED);

    }


    List<String> excelname = Arrays.asList(".xls",".xlsx");
    @RequestMapping("/upLoadExcel")
    public ResultEntity  upLoadExcel(MultipartFile file) throws Exception {
        if (file == null){
            throw new Exception("文件为空");
        }
        String fileType = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));

        if (!excelname.contains(fileType)){
            throw new Exception("只能导入excel格式的文件");
        }
        try {
            this.iEcKeywordService.uplodExcel(file);
        }catch (Exception e){
            e.printStackTrace();
            log.error(e.getMessage());
            throw new Exception("导入数据失败");
        }
        return ResultEntity.ok("导入数据成功");
    }
}
